﻿Public Class ThongTinNhaDAO

    Private connect As ConnectDB
    Private sqlstr As String


    Public Sub New()
        connect = New ConnectDB()
    End Sub

    Public Function IsUpTT(ByVal emp As ThongTinNhaDTO, ByVal flag As Boolean)
        Dim matt As String = emp.Mã_TTN
        Dim madt As String = emp.Mã_đăng_tin
        Dim mal As String = emp.Mã_loại_nhà
        Dim makv As String = emp.Mã_khu_vực
        Dim dc As String = emp.Địa_chỉ
        Dim dt As String = emp.Diện_tích
        Dim gt As String = emp.Giá_tiền
        Dim mt As String = emp.Mô_tả
        Dim sc As String = emp.Sức_chứa
        Dim hn As String = emp.Hướng_nhà
        Dim nd As Date = emp.Ngày_đăng
        Dim nh As Date = emp.Ngày_hết_hạn

        '-------------------------------------------'
        If flag = True Then
            sqlstr = "INSERT THONGTINNHA (MaDangTin, MaLoai, MaKhuVuc, DiaChiNha, DienTich, GiaTien, Mota, SucChua, HuongNha, NgayDang, NgayHetHan)" & _
                        " VALUES('" & madt & "'," & mal & "'," & makv & "'," & dc & "'," & dt & "'," & gt & "'," & mt & "'," & sc & "'," & hn & "'," & nd & "'," & nh & "')"
        Else
            sqlstr = "UPDATE THONGTINNHA SET MaDangTin   = '" & madt & _
                                           "', MaLoai   = '" & mal & _
                                           "', MaKhuVuc   = '" & makv & _
                                           "', DiaChiNha   = '" & dc & _
                                           "', DienTich   = '" & dt & _
                                           "', GiaTien   = '" & gt & _
                                           "', MoTa   = '" & mt & _
                                           "', SucChua   = '" & sc & _
                                           "', HuongNha   = '" & hn & _
                                           "', NgayDang   = '" & nd & _
                                           "', NgayHetHan   = '" & nh & _
                                    "' WHERE MaThongTinNha  = '" & matt & "'"

        End If
        Return connect.Execute(sqlstr)
    End Function

    Public Function DeleteTT(ByVal emp As ThongTinNhaDTO)
        Dim IDKV As String = emp.Mã_TTN
        sqlstr = "DELETE  THONGTINNHA MaThongTinNha = '" & IDKV & "'"
        Return connect.Execute(sqlstr)
    End Function

    Public Function Getdata() As List(Of ThongTinNhaDTO)
        Dim i As Integer
        Dim maTT, maDT, maLN, maKV, dc, dtich, gt, mt, sc, hn As String
        Dim nd, nhh As Date
        Dim emp As ThongTinNhaDTO
        Dim nnList As New List(Of ThongTinNhaDTO)
        Dim dt As New DataTable
        dt = connect.GetData("Select * From THONGTINNHA")

        For i = 0 To dt.Rows.Count() - 1
            maTT = dt.Rows(i)("MaThongTinNha").ToString
            maDT = dt.Rows(i)("MaDangTin").ToString
            maLN = dt.Rows(i)("MaLoai").ToString
            maKV = dt.Rows(i)("MaKhuVuc").ToString
            dc = dt.Rows(i)("DiaChiNha").ToString
            dtich = dt.Rows(i)("DienTich").ToString
            gt = dt.Rows(i)("GiaTien").ToString
            mt = dt.Rows(i)("MoTa").ToString
            sc = dt.Rows(i)("SucChua").ToString
            hn = dt.Rows(i)("HuongNha").ToString
            nd = dt.Rows(i)("NgayDang").ToString
            nhh = dt.Rows(i)("NgayHetHan").ToString

            emp = New ThongTinNhaDTO(maTT, maDT, maLN, maKV, dc, dtich, gt, mt, sc, hn, nd, nhh)
            nnList.Add(emp)
        Next
        Return nnList
    End Function

End Class
